package com.ls.lserp.modules.order.service;


import java.io.IOException;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CacheUtils;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.dbaccess.Entity.ContractIndentEntity;
import com.ls.lserp.modules.order.dao.AuditInfoDao;
import com.ls.lserp.modules.order.dao.BigConsumptionDao;
import com.ls.lserp.modules.order.dao.OrderMaterielDao;
import com.ls.lserp.modules.order.entity.AuditInfo;
import com.ls.lserp.modules.order.entity.BigConsumption;
import com.ls.lserp.modules.order.entity.MaterialPurchase;
import com.ls.lserp.modules.order.entity.OrderMateriel;
import com.ls.lserp.modules.sys.dao.UserDao;
import com.ls.lserp.modules.sys.entity.User;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
import com.ls.lserp.modules.sys.utils.UserUtils;

import com.ls.lserp.common.utils.excel.template.ExcelTemplate;

/**
 * @ClassName: OrderMaterielService
 * @Description: 订单核料
 * 
 * @author 
 * @date 2017年02月20日
 *
 */

@Service
@Transactional(readOnly = false)
public class OrderMaterielService extends CrudService<OrderMaterielDao, OrderMateriel> {
	@Autowired

	// 分类方法接口
	private OrderMaterielDao orderMaterielDao;
	
    @Autowired
    private UserDao userDao;
    
    // 大货单耗
    @Autowired
    private BigConsumptionDao bigConsumptionDao;
    
    // 审核表
    @Autowired
    private AuditInfoDao  auditInfoDao;
	
	/**
	 * @Title: findList
	 * @Description: 一览查询
	 * @param page
	 * @param orderMateriel
	 * @return Page<OrderMateriel>
	 */
	public Page<OrderMateriel> findList(Page<OrderMateriel> page, OrderMateriel orderMateriel) {
		
		// tab页分，检索条件设定(0:全部,1:待审核,2:审核通过,3:审核拒绝)
		int tabFlg = orderMateriel.getTabPageStatus();
		if(1==tabFlg){
			orderMateriel.setReserveMeterialStatus(ResourcesUtil.getConfig("reserver_meterial_wait_accept"));
		}else if(2==tabFlg){
			orderMateriel.setReserveMeterialStatus(ResourcesUtil.getConfig("reserver_meterial_ok"));
		}else if(3==tabFlg){
			orderMateriel.setReserveMeterialStatus(ResourcesUtil.getConfig("reserver_meterial_no"));
		}else{
			orderMateriel.setReserveMeterialStatus("");
		}
		
		// 设置分页参数
		orderMateriel.setPage(page);
		
		orderMateriel.setAuditPhaseCd(ResourcesUtil.getConfig("audit_phase_material"));
		List<OrderMateriel> resultList = orderMaterielDao.findList(orderMateriel);
		
		for(OrderMateriel o : resultList){
			o.setAuditUserName(getUserAllName(o.getAuditUserId()));
		}
		
		// 执行分页查询
		page.setList(resultList);
		return page;
	}
	
    /**
     * @Title: getOrderMateriel
     * @Description: 获取订单核料数据
     * @param @param orderMateriel
     * @param @return
     * @return OrderMateriel 
     * @throws
     */
   
   public OrderMateriel getShowInfo(OrderMateriel orderMateriel){
	       
	    // 大货单耗 信息取得
	    OrderMateriel resultUnitCost = orderMaterielDao.getEditUnitCostInfo(orderMateriel.getId());
	    // 大货单耗号
	    orderMateriel.setUnitCostNo(resultUnitCost.getUnitCostNo());
	    // 大货单耗成本
	    orderMateriel.setUnitCost(resultUnitCost.getUnitCost());
	    // 物料数
	    orderMateriel.setMaterialNum(resultUnitCost.getMaterialNum());
	    // 物料id
	    orderMateriel.setUnitCostId(resultUnitCost.getUnitCostId());
	    // 设定大货下拉列表
	 	List<BigConsumption> resultUnitCostList = bigConsumptionDao.findUnitCostList(orderMateriel.getStyleId());
	 	
	 	if(!CollectionsUtils.isEmpty(resultUnitCostList)){
			// 大货单耗下拉列表设定
	 		orderMateriel.setUnitCostList(resultUnitCostList);
	 	}
	    
	    
		List<OrderMateriel> resultOrderMaterialList = orderMaterielDao.findEditMaterialList(orderMateriel.getId());
		if(!CollectionsUtils.isEmpty(resultOrderMaterialList)){
			orderMateriel.setOrderMaterielList(resultOrderMaterialList);
			List<MaterialPurchase> quantityList = null;
			List<MaterialPurchase> quantityPurList = null;
			List<MaterialPurchase> quantityResList = null;
			BigDecimal allQuantity = null;
			MaterialPurchase venderForm = null;
			OrderMateriel paramVender = null;
			for(OrderMateriel m : resultOrderMaterialList){
				// 预订耗料数
				//m.setPlanMaterialLoss((checkBigDecimalNull(m.getMaterialLoss()).multiply(checkBigDecimalNull(m.getIndentQuantity()))).setScale(2, BigDecimal.ROUND_HALF_UP));
				quantityPurList = orderMaterielDao.findEditOrderMaterialList(m.getMaterialReserveId());
				quantityResList = orderMaterielDao.findMaterialReserveList(m.getMaterialReserveId());
				if (quantityResList==null) {
					quantityResList = new ArrayList<>();
				}
				quantityResList.addAll(quantityPurList);
				allQuantity = new BigDecimal("0");
				for(MaterialPurchase mp : quantityResList){
					if (mp.getVenderId() != 1 && mp.getVenderId() != 99) {
						allQuantity = allQuantity.add(checkBigDecimalNull(mp.getPurchaseReserveQuantity()));
					}
					// 净单价(退税*单价)
					if (!"0.000".equals(checkBigDecimalNull(mp.getPurchaseTaxRebate()).toString())&&!"0.00000".equals(checkBigDecimalNull(mp.getPurchaseTaxRebate()).toString())&&mp.getPurchaseTaxRebate()!=null) {
						mp.setPurchaseUnitCost((checkBigDecimalNull(mp.getPurchaseUnitPrice()).multiply(checkBigDecimalNull(mp.getPurchaseTaxRebate()))).setScale(2, BigDecimal.ROUND_HALF_UP));
					}else {
						mp.setPurchaseTaxRebate(new BigDecimal(0));
						mp.setPurchaseUnitCost((checkBigDecimalNull(mp.getPurchaseUnitPrice())).setScale(2, BigDecimal.ROUND_HALF_UP));
					}
					// 金额(数量*净单价)
					mp.setPurchasePrice((checkBigDecimalNull(mp.getPurchaseReserveQuantity()).multiply(checkBigDecimalNull(mp.getPurchaseUnitCost()))).setScale(2, BigDecimal.ROUND_HALF_UP));
				}
				// 采购数量
				m.setPurchaseReserveQuantity(allQuantity);
				
				// 设定下拉列表供货商
	    		paramVender = new OrderMateriel();
	    		paramVender.setMaterialId(m.getMaterialId());
	    		List<OrderMateriel> resultVender = orderMaterielDao.findVenderlList(paramVender);
	    		// 设定供货商下拉列表
	    		m.setSelectVenderList(resultVender);
				
				// 设定供货商明细
				venderForm = new MaterialPurchase();
				venderForm.setMaterialPurchaseList(quantityResList);
				m.setVenderForm(venderForm);
			}
			
		}	
       
    return orderMateriel;
   }
   
	/**
	 * 根据ID获取用户
	 * @param id
	 * @return 取不到返回null
	 */
	public String getUserAllName(long id){
		if(id==0){
			return "";
		}
		User user = (User)CacheUtils.get(UserUtils.USER_CACHE, UserUtils.USER_CACHE_ID_ + id);
		if (user ==  null){
			user = userDao.get(id);
			if (user == null){
				return "";
			}
		}
		return user.getFirstName()+user.getLastName();
	}
	
	/**
	 * check BigDecimal
	 * @param cost
	 * @return 取不到返回0
	 */
	public BigDecimal checkBigDecimalNull(BigDecimal cost){
		if(cost==null){
			return new BigDecimal("0");
		}
		return cost;
	}
	
	/**
	 * @Title: 合同订单选择一览
	 * @Description: 一览查询
	 * @param page
	 * @param orderMateriel
	 * @return Page<OrderMateriel>
	 */
	public Page<OrderMateriel> findIndentList(Page<OrderMateriel> page, OrderMateriel orderMateriel) {
		
		// 设置分页参数
		orderMateriel.setPage(page);
		
		// 执行分页查询
		page.setList(orderMaterielDao.findIndentList(orderMateriel));
		return page;
	}
	
    /**
     * @Title: getCreateOrderMaterielInfo
     * @Description: 获取新建时获取核料信息
     * @param @param indentId
     * @param @return
     * @return OrderMateriel 
     * @throws
     */
   
   public OrderMateriel getCreateOrderMaterielInfo(Long indentId){
   	
   	// 实力化实体类
	OrderMateriel resultOrderMateriel = orderMaterielDao.getCreateOrderMaterielInfo(indentId);
	
	if(resultOrderMateriel.getStyleId()!=null){
		
		// 通过款式id取得大货单耗一览信息
		List<BigConsumption> resultUnitCostList = bigConsumptionDao.findUnitCostList(resultOrderMateriel.getStyleId());
		if(!CollectionsUtils.isEmpty(resultUnitCostList)){
			// 大货单耗下拉列表设定
			resultOrderMateriel.setUnitCostList(resultUnitCostList);
			
			// 新建时，设定大耗单价
			if(StringUtils.isNotBlank(resultUnitCostList.get(0).getUnitCost())){
				resultOrderMateriel.setUnitCost(new BigDecimal(resultUnitCostList.get(0).getUnitCost()));
			}else{
				resultOrderMateriel.setUnitCost(new BigDecimal("0"));
			}
			
			// 设定大货单耗id
			if(resultUnitCostList.get(0).getId()!=null){
			    resultOrderMateriel.setUnitCostId(resultUnitCostList.get(0).getId());
			    
			    // 通过款式id和大货单耗id取得物料一览
			    OrderMateriel paramOrderMateriel = new OrderMateriel();
			    paramOrderMateriel.setStyleId(resultOrderMateriel.getStyleId());
			    paramOrderMateriel.setUnitCostId(resultOrderMateriel.getUnitCostId());
			    // 实力化实体类
			    List<OrderMateriel> resultFindAllMaterial = orderMaterielDao.findAllMaterialByStyle(paramOrderMateriel);
			    if(!CollectionsUtils.isEmpty(resultFindAllMaterial)){
			    	// 设定物料一览
			    	resultOrderMateriel.setOrderMaterielList(resultFindAllMaterial);
			    	// 物料数
			    	resultOrderMateriel.setMaterialNum(resultFindAllMaterial.size());

			    	OrderMateriel paramVender = null;
			    	for(OrderMateriel o : resultOrderMateriel.getOrderMaterielList()){
			    		paramVender = new OrderMateriel();
			    		paramVender.setMaterialId(o.getMaterialId());
			    		List<OrderMateriel> resultVender = orderMaterielDao.findVenderlList(paramVender);
			    		// 设定供货商下拉列表
			    		o.setSelectVenderList(resultVender);
			    		// 初始化库存数量
			    		o.setPurchaseReserveQuantity(new BigDecimal("0"));
			    		// 预订耗料数
						o.setPlanMaterialLoss((checkBigDecimalNull(o.getMaterialLoss()).multiply(checkBigDecimalNull(resultOrderMateriel.getIndentQuantity()))).setScale(2, BigDecimal.ROUND_HALF_UP));
						// 初始化明细link
						o.setOrderMaterielDetail(0);
			    	}
			    }
			    
			}
		}
		
	}
	   
	return resultOrderMateriel;
   }
   
   /**
	 * @Title: 获取供货商信息
	 * @Description: 一览查询
	 * @param materialId
	 * @return List<OrderMateriel>
	 */
	public List<OrderMateriel> findVenderList(OrderMateriel paramVender) {
		return orderMaterielDao.findVenderlList(paramVender);
	}
	
	/**
	 * @Title: 通过面料ID获取已预订库存
	 * @Description: 一览查询
	 * @param materialId
	 * @return List<OrderMateriel>
	 */
	public BigDecimal sumMaterialQuantity(OrderMateriel orderMateriel) {
		BigDecimal unusableMQuantity = orderMaterielDao.sumMaterialQuantity(orderMateriel);
		if (unusableMQuantity==null) {
			unusableMQuantity = new BigDecimal(0);
		}
		return unusableMQuantity;
	}
   
	/**
	 * @Title: 采购履历信息取得
	 * @Description: 一览查询
	 * @param materialId
	 * @return List<OrderMateriel>
	 */
	public List<OrderMateriel> findContractMeterialList(Long materialId) {
		OrderMateriel param = new OrderMateriel();
		param.setMaterialId(materialId);
		return orderMaterielDao.findContractMeterialList(param);
	}
	
    /**
     * @Title: getCreateOrderMaterielInfo
     * @Description: 获取新建时获取核料信息
     * @param @param indentId
     * @param @return
     * @return OrderMateriel 
     * @throws
     */
   
   public OrderMateriel getSelectOrderMaterielInfo(OrderMateriel orderMateriel){
    OrderMateriel resultOrderMateriel = new OrderMateriel();    
	
	// 单耗成本取得
	BigConsumption bUnitCost = bigConsumptionDao.getRealUnitCost(orderMateriel.getUnitCostId());
	if(bUnitCost!=null){
		if(StringUtils.isNotBlank(bUnitCost.getUnitCost())){
			resultOrderMateriel.setUnitCost(new BigDecimal(bUnitCost.getUnitCost()));
		}else{
			resultOrderMateriel.setUnitCost(new BigDecimal("0"));
		}
	}
	
    // 通过款式id和大货单耗id取得物料一览
    OrderMateriel paramOrderMateriel = new OrderMateriel();	
    paramOrderMateriel.setStyleId(orderMateriel.getStyleId());
    paramOrderMateriel.setUnitCostId(orderMateriel.getUnitCostId());
    // 实力化实体类
    List<OrderMateriel> resultFindAllMaterial = orderMaterielDao.findAllMaterialByStyle(paramOrderMateriel);
    
    if(!CollectionsUtils.isEmpty(resultFindAllMaterial)){
    	resultOrderMateriel.setOrderMaterielList(resultFindAllMaterial);
    	resultOrderMateriel.setMaterialNum(resultFindAllMaterial.size());
    	
    	// 供货商list
    	OrderMateriel paramVender = null;
    	for(OrderMateriel o : resultOrderMateriel.getOrderMaterielList()){
    		paramVender = new OrderMateriel();
    		paramVender.setMaterialId(o.getMaterialId());
    		List<OrderMateriel> resultVender = orderMaterielDao.findVenderlList(paramVender);
    		o.setSelectVenderList(resultVender);
    		
    		//采购数量
    		o.setPurchaseReserveQuantity(new BigDecimal("0"));
    		// 预订耗料数
			o.setPlanMaterialLoss((checkBigDecimalNull(o.getMaterialLoss()).multiply(checkBigDecimalNull(orderMateriel.getIndentQuantity()))).setScale(2, BigDecimal.ROUND_HALF_UP));
			// 初始化明细link
			o.setOrderMaterielDetail(0);
    	}
    }

	return resultOrderMateriel;
   }
   
	/**
	  * @Title: saveDict
	  * @Description: 保存基准数据
	  * @param @param dict
	  * @return void 
	  * @throws
	  */
	@Transactional(readOnly = false)
	public void saveOrderMateriel(OrderMateriel orderMateriel) {
		
		// 判断新建编辑
		//若为null 执行插入语句（新建）
		if (orderMateriel.getId()==null){
			orderMateriel.preInsert();
			//取得核料编号
			orderMateriel.setReserverNo(SequenceUtil.offerCode(OrderMateriel.ORDER_MATERIEL_PHASE_CD,OrderMateriel.SEQUENCE_NUCLEARMATERIAL,2,orderMateriel.getStyleNo()).getReturnCode());
			//设置核料状态
			orderMateriel.setReserveMeterialStatus(ResourcesUtil.getConfig("reserver_meterial_wait_accept"));
			orderMaterielDao.insert(orderMateriel);
		}else{
			
			List<OrderMateriel> orderMaterielIdList = orderMaterielDao.findAllReserverMaterialId(orderMateriel.getId());
			// 删除核料供货商表
			for(OrderMateriel om : orderMaterielIdList){
				if(om.getMaterialReserveId()!=null){
					// 物理删除预采购明细表
					orderMaterielDao.physicallyDeletePurchaseS(om.getMaterialReserveId());
					// 物理删除预定库存表
					orderMaterielDao.physicallyDeleteReserveS(om.getMaterialReserveId());
				}
			}
			// 删除核料辅料表
			orderMaterielDao.physicallyDeleteReserveMaterialS(orderMateriel.getId());
			
			// 更新大货单耗
			orderMateriel.preUpdate();
			orderMaterielDao.updateUnitCost(orderMateriel);
		}	
		OrderMateriel paramStyleMaterial = null;
		OrderMateriel resultOrderMater = null;
		if (orderMateriel.getOrderMaterielList()!=null&&orderMateriel.getOrderMaterielList().size()>0) {
			for(OrderMateriel om : orderMateriel.getOrderMaterielList()){
				paramStyleMaterial = new OrderMateriel();
				paramStyleMaterial.setMaterialId(om.getMaterialId());
				paramStyleMaterial.setUnitCostId(orderMateriel.getUnitCostId());
				paramStyleMaterial.setStyleId(orderMateriel.getStyleId());
				// 款式物料表
				//OrderMateriel resultOrderMater = orderMaterielDao.getStyleMaterialS(paramStyleMaterial);
				resultOrderMater = new OrderMateriel();
				if(resultOrderMater!=null){
					resultOrderMater.preInsert();
					resultOrderMater.setVersion(1L);
					resultOrderMater.setMaterialId(om.getMaterialId());
					resultOrderMater.setId(orderMateriel.getId());
					resultOrderMater.setContractId(orderMateriel.getContractId());
					resultOrderMater.setIndentId(orderMateriel.getIndentId());
					resultOrderMater.setStyleId(orderMateriel.getStyleId());
					resultOrderMater.setPhaseCd(ResourcesUtil.getConfig("audit_phase_material"));
					resultOrderMater.setPurchaseReserveQuantity(om.getPurchaseReserveQuantity());
					resultOrderMater.setMaterialCost(om.getMaterialCost());
					resultOrderMater.setMaterialPreLoss(om.getMaterialPreLoss());
					resultOrderMater.setMaterialLoss(om.getMaterialLoss());
					resultOrderMater.setIndentQuantity(om.getIndentQuantity());
					resultOrderMater.setPlanMaterialLoss(om.getPlanMaterialLoss());
					// 核料辅料表
					orderMaterielDao.insertReserveMaterialS(resultOrderMater);
					
					MaterialPurchase venderForm = om.getVenderForm();
					if(venderForm!=null){
						for(MaterialPurchase mp : venderForm.getMaterialPurchaseList()){
							if(mp.getVenderId()!=null){
								mp.preInsert();
								mp.setVersion(1L);
								mp.setMaterialReserveId(resultOrderMater.getMaterialReserveId());
								mp.setMaterialId(om.getMaterialId());
								mp.setContractId(orderMateriel.getContractId());
								mp.setIndentId(orderMateriel.getIndentId());
								mp.setStyleId(orderMateriel.getStyleId());
								if (mp.getVenderId()!=1 && mp.getVenderId()!=99) {
									//物料供货商表
									orderMaterielDao.insertMaterialPurchaseS(mp);
								}else {
									//预订物料表
									orderMaterielDao.insertMaterialReserveS(mp);
								}
							}
						}
					}
				}
	        }
			String breakFlag = "";
			ContractIndentEntity cEntity = new ContractIndentEntity();
			cEntity.setUpdateDateTime(new Date());
			cEntity.setUpdateUserId(UserUtils.getUser().getId());
			cEntity.setIndentId(orderMateriel.getIndentId());
			for (OrderMateriel om : orderMateriel.getOrderMaterielList()) {
				MaterialPurchase venderForm = om.getVenderForm();
				if (venderForm != null) {
					for (MaterialPurchase mp : venderForm.getMaterialPurchaseList()) {
						if (mp.getVenderId() != null) {
							if (mp.getVenderId() != 1 && mp.getVenderId()!=99) {
								// 物料供货商表
								cEntity.setStockStatus(ResourcesUtil.getConfig("notice_ns00"));
								orderMaterielDao.updateIndentStatus(cEntity);
								breakFlag = "cloud";
								break;
							}
						}
					}
					if ("cloud".equals(breakFlag)) {
						break;
					}
				}
	        }
			if (!"cloud".equals(breakFlag)) {
				cEntity.setStockStatus(ResourcesUtil.getConfig("notice_ns10"));
				orderMaterielDao.updateIndentStatus(cEntity);
			}
			
		}
	}
	
    /**
     * @Title: findAuditList
     * @Description: 查询所有履历
     * @param @param auditInfo
     * @param @return
     * @return List<AuditInfo> 
     * @throws
     */
   public AuditInfo findAuditInfo(AuditInfo auditInfo) {
	   AuditInfo audit = new AuditInfo();
	   audit.setAuditList(auditInfoDao.findAllList(auditInfo));
	   audit.setAuditStatus(auditInfoDao.findAuditAcceptCount(auditInfo));
       // 执行查询
       return audit;
   }
   
   /**
    * @Title: getAuditResult
    * @Description: 审核结果
    * @param @param auditInfo
    * @param @return
    * @return 审核结果通过数
    * @throws
    */
  
  public long getAuditResult(AuditInfo auditInfo) {
      return auditInfoDao.findAuditAcceptCount(auditInfo);
  }
  
	/**
	  * @Title: saveAudit
	  * @Description: 保存履历信息
	  * @param @param auditInfo
	  * @return void 
	  * @throws
	  */
	@Transactional(readOnly = false)
	public void saveAuditInfo(AuditInfo auditInfo) {
		
		 // 插入履历表
	     auditInfo.preInsert();
	     auditInfo.setAuditDate(new Date());
	     auditInfo.setAuditUserId(UserUtils.getUser().getId());
         // 新建版本号赋值1
	     auditInfo.setVersion(1L);
	     auditInfoDao.insert(auditInfo);
	     
	     // 修正核料生产状态
	     auditInfo.preUpdate();
	     orderMaterielDao.updateOrderMaterialStatus(auditInfo);
	}
	
	/**
	  * @Title: hasOrderMaterial
	  * @Description: 通过合同订单，款式，大货单耗,检索核料信息表
	  * @param @param itemValue
	  * @param @param itemCode
	  * @param @return
	  * @return boolean 
	  * @throws
	  */
	
	public boolean hasOrderMaterial(OrderMateriel orderMateriel) {
		// 没有核料数据
		boolean hasOrderMaterial = false;	
		
		List<OrderMateriel> resultOrderMaterielList = orderMaterielDao.findReserveMeterialP(orderMateriel);
		if(!CollectionsUtils.isEmpty(resultOrderMaterielList)){
			int orderMaterialCount = resultOrderMaterielList.size();
			
			// 编辑时
			if(orderMateriel.getId()!=null){
				if(orderMaterialCount>1){
					hasOrderMaterial=true;
				}else if(orderMaterialCount==1){
					// 如果存有1条数据，判断是否是当前对象
					if(!orderMateriel.getId().equals(resultOrderMaterielList.get(0).getId())){
						hasOrderMaterial = true;
					}
				}
			}else{
				if(orderMaterialCount>0){
					hasOrderMaterial = true;
				}
			}
		}
		return hasOrderMaterial;
	}
    /**
     * 导出订单核料
     *
     * @throws IOException
     */
    public ExcelTemplate exportOrderMateriel(OrderMateriel orderMateriel, String tempFilePath) throws IOException {
        // 返回值
        ExcelTemplate handle = new ExcelTemplate();
        // 输出订单基本信息
        printOrderBaseInfoTemp(tempFilePath, orderMateriel, handle);
        // 面辅配料
        printOrderMaterialInfoTemp(tempFilePath, orderMateriel.getOrderMaterielList(), handle);
        
        return handle;

    }
    private void printOrderMaterialInfoTemp(String tempFilePath, List<OrderMateriel> orderMaterielList, ExcelTemplate handle) throws IOException {
        // 变量声明
        List<String> dataCellDeclare = new ArrayList<String>();
        // 变量值集合
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        dataCellDeclare.add("materialColor");
        dataCellDeclare.add("materialUnitCd");
        dataCellDeclare.add("indentQuantityList");
        dataCellDeclare.add("materialSpec");
        dataCellDeclare.add("materialCost");
        dataCellDeclare.add("materialLoss");
        dataCellDeclare.add("planMaterialLoss");
        dataCellDeclare.add("venderNameCn");
        dataCellDeclare.add("purchaseUnitPrice");
        dataCellDeclare.add("purchaseTaxRebate");
        dataCellDeclare.add("purchasePrice");
        dataCellDeclare.add("materialCode");
        dataCellDeclare.add("purchaseReserveQuantity");
        
        // 结果
        Map<String, Object> map;
        // 临时接收变量
        OrderMateriel tempBean;
        MaterialPurchase tempItemBean;
        // 格式化数字
        NumberFormat num = NumberFormat.getInstance();
        for (int i = 0; i < orderMaterielList.size(); i++) {
        	if (orderMaterielList.get(i).getVenderForm() ==null || orderMaterielList.get(i).getVenderForm().getMaterialPurchaseList().size() == 0) {
        		map = new HashMap<String, Object>();
    			tempBean = orderMaterielList.get(i);
    			// 原辅材料
    			if ("".equals(tempBean.getMaterialNameCn()) || tempBean.getMaterialNameCn() == null) {
    				map.put("materialCode", tempBean.getMaterialCode());
    			} else {
    				map.put("materialCode", tempBean.getMaterialCode()+"\n"+tempBean.getMaterialNameCn());
    			}
    			// 面料颜色
    			map.put("materialColor", tempBean.getMaterialColor());
    			// 单位
    			map.put("materialUnitCd", tempBean.getMaterialUnitCd());
    			// 服装数量
    			if (tempBean.getIndentQuantity() == null || "".equals(tempBean.getIndentQuantity())) {
    				map.put("indentQuantityList", tempBean.getIndentQuantity());
    			} else {
    				map.put("indentQuantityList", num.format(tempBean.getIndentQuantity()));
    			}
    			// 幅宽
    			map.put("materialSpec", tempBean.getMaterialSpec());
    			// 单耗
    			if (tempBean.getMaterialCost() == null || "".equals(tempBean.getMaterialCost())) {
    				map.put("materialCost", tempBean.getMaterialCost());
    			} else {
    				map.put("materialCost", num.format(tempBean.getMaterialCost().doubleValue()));
    			}
    			// 含损耗单耗
    			if (tempBean.getMaterialLoss() == null || "".equals(tempBean.getMaterialLoss())) {
    				map.put("materialLoss", tempBean.getMaterialLoss());
    			} else {
    				map.put("materialLoss", num.format(tempBean.getMaterialLoss().doubleValue()));
    			}
    			// 预订耗料数
    			if (tempBean.getPlanMaterialLoss() == null || "".equals(tempBean.getPlanMaterialLoss())) {
    				map.put("planMaterialLoss", tempBean.getPlanMaterialLoss());
    			} else {
    				map.put("planMaterialLoss", num.format(tempBean.getPlanMaterialLoss().doubleValue()));
    			}
    			// 定料数量
    			map.put("purchaseReserveQuantity", tempBean.getPurchaseReserveQuantity());
    			dataList.add(map);
        	} else {
        		for (int j = 0; j < orderMaterielList.get(i).getVenderForm().getMaterialPurchaseList().size(); j++) {
        			map = new HashMap<String, Object>();
        			tempBean = orderMaterielList.get(i);
        			// 原辅材料
        			if ("".equals(tempBean.getMaterialNameCn()) || tempBean.getMaterialNameCn() == null) {
        				map.put("materialCode", tempBean.getMaterialCode());
        			} else {
        				map.put("materialCode", tempBean.getMaterialCode()+"\n"+tempBean.getMaterialNameCn());
        			}
        			// 面料颜色
        			map.put("materialColor", tempBean.getMaterialColor());
        			// 单位
        			map.put("materialUnitCd", tempBean.getMaterialUnitCd());
        			// 服装数量
        			if (tempBean.getIndentQuantity() == null || "".equals(tempBean.getIndentQuantity())) {
        				map.put("indentQuantityList", tempBean.getIndentQuantity());
        			} else {
        				map.put("indentQuantityList", num.format(tempBean.getIndentQuantity()));
        			}
        			// 幅宽
        			map.put("materialSpec", tempBean.getMaterialSpec());
        			// 单耗
        			if (tempBean.getMaterialCost() == null || "".equals(tempBean.getMaterialCost())) {
        				map.put("materialCost", tempBean.getMaterialCost());
        			} else {
        				map.put("materialCost", num.format(tempBean.getMaterialCost().doubleValue()));
        			}
        			// 含损耗单耗
        			if (tempBean.getMaterialLoss() == null || "".equals(tempBean.getMaterialLoss())) {
        				map.put("materialLoss", tempBean.getMaterialLoss());
        			} else {
        				map.put("materialLoss", num.format(tempBean.getMaterialLoss().doubleValue()));
        			}
        			// 预订耗料数
        			if (tempBean.getPlanMaterialLoss() == null || "".equals(tempBean.getPlanMaterialLoss())) {
        				map.put("planMaterialLoss", tempBean.getPlanMaterialLoss());
        			} else {
        				map.put("planMaterialLoss", num.format(tempBean.getPlanMaterialLoss().doubleValue()));
        			}
        			// 定料数量
        			if (tempBean.getPurchaseReserveQuantity() == null || "".equals(tempBean.getPurchaseReserveQuantity())) {
        				map.put("purchaseReserveQuantity", tempBean.getPurchaseReserveQuantity());
        			}else {
        				map.put("purchaseReserveQuantity", num.format(tempBean.getPurchaseReserveQuantity()));
        			}
        			
        			tempItemBean = orderMaterielList.get(i).getVenderForm().getMaterialPurchaseList().get(j);
        			// 供货商
        			map.put("venderNameCn", tempItemBean.getVenderNameCn());
        			// 单价
        			if (tempItemBean.getPurchaseUnitPrice() == null || "".equals(tempItemBean.getPurchaseUnitPrice())) {
        				map.put("purchaseUnitPrice", tempItemBean.getPurchaseUnitPrice());
        			}else {
        				map.put("purchaseUnitPrice", num.format(tempItemBean.getPurchaseUnitPrice()));
        			}
        			// 退税
        			if (tempItemBean.getPurchaseTaxRebate() == null || "".equals(tempItemBean.getPurchaseTaxRebate())) {
        				map.put("purchaseTaxRebate", tempItemBean.getPurchaseTaxRebate());
        			}else {
        				map.put("purchaseTaxRebate", num.format(tempItemBean.getPurchaseTaxRebate()));
        			}
        			// 金额
        			if (tempItemBean.getPurchasePrice() == null || "".equals(tempItemBean.getPurchasePrice())) {
        				map.put("purchasePrice", tempItemBean.getPurchasePrice());
        			}else {
        				map.put("purchasePrice", num.format(tempItemBean.getPurchasePrice()));
        			}
        			dataList.add(map);
        		}
        	}
        }
        
        // 向excel表输出
        handle.writeListData(tempFilePath, dataCellDeclare, dataList, 0);
    }
    
    private void printOrderBaseInfoTemp(String tempFilePath, OrderMateriel orderMateriel, ExcelTemplate handle) throws IOException {
    	List<String> dataCellDeclare = new ArrayList<String>();
        // 变量值
        Map<String, Object> dataMapResult = new HashMap<String, Object>();

        // 合同单号(核料信息)
        dataCellDeclare.add("indentNo");
        dataMapResult.put("indentNo", orderMateriel.getIndentNo());
        
        // 客户(核料信息)
        dataCellDeclare.add("ognizationName");
        dataMapResult.put("ognizationName", orderMateriel.getOgnizationName());
        
        // 签订日期(核料信息)
        dataCellDeclare.add("indentIssueDate");
        dataMapResult.put("indentIssueDate", DateUtils.formatDate(orderMateriel.getIndentIssueDate(), "yyyy-MM-dd") );
        
        // 款号(核料信息)
        dataCellDeclare.add("styleCustomerNo");
        dataMapResult.put("styleCustomerNo", orderMateriel.getStyleCustomerNo());
        
        // 款式名称(核料信息)
        dataCellDeclare.add("styleNameCn");
        dataMapResult.put("styleNameCn", orderMateriel.getStyleNameCn());
        
        // 交货日期(核料信息)
        dataCellDeclare.add("indentDeliveryDate");
        dataMapResult.put("indentDeliveryDate", DateUtils.formatDate(orderMateriel.getIndentDeliveryDate(), "yyyy-MM-dd"));
        
        // 订单数量(核料信息)
        dataCellDeclare.add("indentQuantity");
        dataMapResult.put("indentQuantity", orderMateriel.getIndentQuantity());
        
        // 单耗号(核料信息)
        dataCellDeclare.add("unitCostNo");
        dataMapResult.put("unitCostNo", orderMateriel.getUnitCostNo());
        
        // 单耗成本(核料信息)
        dataCellDeclare.add("unitCost");
        dataMapResult.put("unitCost", orderMateriel.getUnitCost());
        
        // 物料数量(核料信息)
        dataCellDeclare.add("materialNum");
        dataMapResult.put("materialNum", orderMateriel.getMaterialNum());
        
        // 向excel表输出
        handle.writeData(tempFilePath, dataCellDeclare, dataMapResult, 0);
    }
}
